# -*- coding: utf-8 -*-
"""
Created on Wed May 29 10:38:00 2019

@author: Administrator
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor as dtr  #回归树
from sklearn.tree import DecisionTreeClassifier as dtc #分类树
import sklearn.datasets as dts
#
#生成回归数据
x=np.linspace(0,5,100).reshape(-1,1)     #生成400个，0-4之间的数据
y=np.sin(x)     #y是x的sin函数值

#建深度为5的回归树模型
r5=dtr(max_depth=5)
r5.fit(x,y)
h5=r5.predict(x)

#输出
print('回归精度为:',r5.score(x,y))
#画图
plt.scatter(x,y,label='真实值',c='b')
plt.plot(x,h5,c='r',label='预测值')
plt.show()

#----------------------------------------------------
#用分类树进行分类
#加载数据
data=dts.load_breast_cancer()
x=data.data

y=data.target

# #建立分类树模型
c5=dtc(max_depth=5)
c5.fit(x,y)
print('分类树精度:',c5.score(x,y))





